package br.com.beautycenter.view;

import br.com.beautycenter.DAO.PersisteCliente;
import br.com.beautycenter.DAO.PersistePacoteTratamento;
import br.com.beautycenter.ModelosTabela.TableModelPacotes;
import br.com.beautycenter.bean.Cliente;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class HistoricoPacotes extends javax.swing.JFrame {
    
    PersisteCliente persisteCliente = new PersisteCliente();
    PersistePacoteTratamento persistePacote = new PersistePacoteTratamento();
    
    Cliente cliente = new Cliente();
    
    List<Cliente> listaClientes = new ArrayList<Cliente>();
    
    
    JTable tabelaPacote, tabelaPilates, tabelaLaser, tabelaAvulso;
    
    TableModelPacotes 
        modelPacote = new TableModelPacotes(),
        modelPilates = new TableModelPacotes(),
        modelLaser = new TableModelPacotes(),
        modelAvulso = new TableModelPacotes();
    
    public void iniciarComponentes(){
        criarTabelas();
        txtCliente.setText("");
    }
    
    public void pesquisar(Cliente parametro){
        cliente = parametro;
        preencherTabelas();
    }
    
    public void pesquisar(){
        String nome = txtCliente.getText().trim();
        
        if(nome.isEmpty()){
            JOptionPane.showMessageDialog(null, "Informe o nome do cliente.");
        }else{
            cliente = new Cliente();
            cliente.setNome(nome);
            listaClientes = persisteCliente.retornarClientePeloNome(cliente);
            if(listaClientes.isEmpty()){
                JOptionPane.showMessageDialog(null, "Não há cliente com o nome informado.");
            }else{
                //exibe o modal
                SelecionarPaciente selecionarCliente = new SelecionarPaciente(this, true);
                selecionarCliente.setLocationRelativeTo(null);
                selecionarCliente.setVisible(true);
                
                //apos concluir o modal, exibe a tabela, caso tenha selecionado algum cliente
                if(cliente != null) preencherTabelas();
                
            }
        }
    }
    
    /**
     * Esse método será executando dar um duplo click sobre um item da tabela
     * @param tabela - Tabela que recebeu o duplo clique
     * @param modelo - modelo referente à tabela que recebeu o duplo clique
     */
    public void exibirPacote(JTable tabela, TableModelPacotes modelo){
        int linha = tabela.getSelectedRow();
        modelo.exibirPacote(linha);
    }

    public void criarTabelas(){
        tabelaPacote = new JTable(modelPacote);
        tabelaPacote.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getClickCount() == 2){
                    exibirPacote(tabelaPacote, modelPacote);
                }
            }
        });
        
        tabelaPilates = new JTable(modelPilates);
        tabelaPilates.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getClickCount() == 2){
                    exibirPacote(tabelaPilates, modelPilates);
                }
            }
        });
        
        tabelaLaser = new JTable(modelLaser);
        tabelaLaser.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getClickCount() == 2){
                    exibirPacote(tabelaLaser, modelLaser);
                }
            }
        });
        
        tabelaAvulso = new JTable(modelAvulso);
        tabelaAvulso.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getClickCount() == 2){
                    exibirPacote(tabelaAvulso, modelAvulso);
                }
            }
        });
        
        JScrollPane scrollPacote = new JScrollPane(tabelaPacote, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                                           JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPacote.setPreferredSize(new Dimension(501, 181));
        
        JScrollPane scrollPilates = new JScrollPane(tabelaPilates, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                                           JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPilates.setPreferredSize(new Dimension(501, 181));
        
        JScrollPane scrollLaser = new JScrollPane(tabelaLaser, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                                           JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollLaser.setPreferredSize(new Dimension(501, 181));
        
        JScrollPane scrollAvulso = new JScrollPane(tabelaAvulso, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                                           JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollAvulso.setPreferredSize(new Dimension(501, 181));
        
        
        jPPacoteTratamento.setLayout(new BorderLayout());
        jPPacoteTratamento.add(scrollPacote, BorderLayout.PAGE_START);
        
        jPPilates.setLayout(new BorderLayout());
        jPPilates.add(scrollPilates, BorderLayout.PAGE_START);
        
        jPLaser.setLayout(new BorderLayout());
        jPLaser.add(scrollLaser, BorderLayout.PAGE_START);
        
        jPAvulso.setLayout(new BorderLayout());
        jPAvulso.add(scrollAvulso, BorderLayout.PAGE_START);
    }
    
    
    public void preencherTabelas(){
        modelPacote.clear();
        modelPacote.addListaPacotes(persistePacote.retornarPacotesPeloTipoCliente(1, cliente));
        
        modelPilates.clear();
        modelPilates.addListaPacotes(persistePacote.retornarPacotesPeloTipoCliente(2, cliente));
        
        modelLaser.clear();
        modelLaser.addListaPacotes(persistePacote.retornarPacotesPeloTipoCliente(3, cliente));
        
        modelAvulso.clear();
        modelAvulso.addListaPacotes(persistePacote.retornarPacotesPeloTipoCliente(4, cliente));
    }

    public HistoricoPacotes() {
        initComponents();
        iniciarComponentes();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPCliente = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtCliente = new javax.swing.JTextField();
        btnPesquisar = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jPPacoteTratamento = new javax.swing.JPanel();
        jPPilates = new javax.swing.JPanel();
        jPLaser = new javax.swing.JPanel();
        jPAvulso = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Histórico de Pacotes");

        jPCliente.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Pesquise pelo cliente desejado", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        jLabel1.setText("Cliente");

        txtCliente.setToolTipText("Digite o nome do cliente (pode ser parcialmente) e aperte enter");
        txtCliente.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtClienteKeyPressed(evt);
            }
        });

        btnPesquisar.setText("Pesquisar");
        btnPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPesquisarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPClienteLayout = new javax.swing.GroupLayout(jPCliente);
        jPCliente.setLayout(jPClienteLayout);
        jPClienteLayout.setHorizontalGroup(
            jPClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPClienteLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnPesquisar)
                .addContainerGap(584, Short.MAX_VALUE))
        );
        jPClienteLayout.setVerticalGroup(
            jPClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPClienteLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnPesquisar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPPacoteTratamento.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Pacotes de Tratamento", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        javax.swing.GroupLayout jPPacoteTratamentoLayout = new javax.swing.GroupLayout(jPPacoteTratamento);
        jPPacoteTratamento.setLayout(jPPacoteTratamentoLayout);
        jPPacoteTratamentoLayout.setHorizontalGroup(
            jPPacoteTratamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPPacoteTratamentoLayout.setVerticalGroup(
            jPPacoteTratamentoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        jPPilates.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Pilates", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        javax.swing.GroupLayout jPPilatesLayout = new javax.swing.GroupLayout(jPPilates);
        jPPilates.setLayout(jPPilatesLayout);
        jPPilatesLayout.setHorizontalGroup(
            jPPilatesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 489, Short.MAX_VALUE)
        );
        jPPilatesLayout.setVerticalGroup(
            jPPilatesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 158, Short.MAX_VALUE)
        );

        jPLaser.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Laser", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        javax.swing.GroupLayout jPLaserLayout = new javax.swing.GroupLayout(jPLaser);
        jPLaser.setLayout(jPLaserLayout);
        jPLaserLayout.setHorizontalGroup(
            jPLaserLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 489, Short.MAX_VALUE)
        );
        jPLaserLayout.setVerticalGroup(
            jPLaserLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 158, Short.MAX_VALUE)
        );

        jPAvulso.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Avulso", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));
        jPAvulso.setPreferredSize(new java.awt.Dimension(501, 181));

        javax.swing.GroupLayout jPAvulsoLayout = new javax.swing.GroupLayout(jPAvulso);
        jPAvulso.setLayout(jPAvulsoLayout);
        jPAvulsoLayout.setHorizontalGroup(
            jPAvulsoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPAvulsoLayout.setVerticalGroup(
            jPAvulsoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 158, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPCliente, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jSeparator1)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPLaser, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPPacoteTratamento, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPPilates, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPAvulso, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPPilates, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPPacoteTratamento, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPAvulso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPLaser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(45, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jPAvulso, jPLaser});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtClienteKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtClienteKeyPressed
        if(evt.getKeyCode() == KeyEvent.VK_ENTER) pesquisar();
    }//GEN-LAST:event_txtClienteKeyPressed

    private void btnPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPesquisarActionPerformed
        pesquisar();
    }//GEN-LAST:event_btnPesquisarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnPesquisar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPAvulso;
    private javax.swing.JPanel jPCliente;
    private javax.swing.JPanel jPLaser;
    private javax.swing.JPanel jPPacoteTratamento;
    private javax.swing.JPanel jPPilates;
    private javax.swing.JSeparator jSeparator1;
    public javax.swing.JTextField txtCliente;
    // End of variables declaration//GEN-END:variables
}